package com.itheima.ydd.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResultData exceptionHandler(SQLIntegrityConstraintViolationException ex){

        if (ex.getMessage().contains("Duplicate entry")){
            String[] split = ex.getMessage().split(" ");
            return ResultData.error(split[2]+"已存在");
        }
        return ResultData.error("未知错误");
    }

    @ExceptionHandler(CustomException.class)
    public ResultData exceptionHandler(CustomException ex){

        return ResultData.error(ex.getMessage());
    }
}
